1
|
|
|
import { Injectable } from "@angular/core"; |
2
|
|
|
import { AngularFireAuth } from "@angular/fire/auth"; |
3
|
|
|
import { Facebook } from "@ionic-native/facebook/ngx"; |
4
|
|
|
import { Platform } from "@ionic/angular"; |
5
|
|
|
import { auth } from "firebase/app"; |
6
|
|
|
import { UniFirebaseLoginConfigProvider } from "../../config/uni-firebase-login-config-provider"; |
7
|
|
|
import { AbstractAuth } from "../../providers/abstract-auth"; |
8
|
|
|
import { IAuthProvider } from "../../providers/i-auth-provider"; |
9
|
|
|
import { IFacebookAuthOptions } from "./i-facebook-auth-options"; |
10
|
|
|
|
11
|
|
|
@Injectable() |
12
|
|
|
export class FacebookAuth extends AbstractAuth implements IAuthProvider { |
13
|
|
|
public readonly providerKey = "facebook"; |
14
|
|
|
public readonly defaultOptions: IFacebookAuthOptions = { |
15
|
|
|
permissions: ["public_profile"], |
16
|
|
|
scopes: ["public_profile", "email"], |
17
|
|
|
signInType: "popup", |
18
|
|
|
}; |
19
|
|
|
|
20
|
|
|
public constructor( |
21
|
|
|
private facebookAuth: Facebook, |
22
|
|
|
angularFireAuth: AngularFireAuth, |
23
|
|
|
platform: Platform, |
24
|
|
|
config: UniFirebaseLoginConfigProvider, |
25
|
|
|
) { |
26
|
|
|
super(angularFireAuth, platform, config); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public async signInNative(options: any): Promise<auth.UserCredential | null> { |
30
|
|
|
const mergedOptions = Object.assign({}, this.defaultOptions, options); |
31
|
|
|
|
32
|
|
|
const facebookResult = await this.facebookAuth.login(mergedOptions.scopes); |
33
|
|
|
|
34
|
|
|
if (facebookResult.status === "connected") { |
35
|
|
|
return await this.angularFireAuth.auth.signInWithCredential( |
36
|
|
|
auth.FacebookAuthProvider.credential( |
37
|
|
|
facebookResult.authResponse.accessToken, |
38
|
|
|
), |
39
|
|
|
); |
40
|
|
|
} else { |
41
|
|
|
return null; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Fetch user from Facebook API |
47
|
|
|
* |
48
|
|
|
* @param facebookUserId |
49
|
|
|
*/ |
50
|
|
|
public fetchUser(facebookUserId: string): Promise<any> { |
51
|
|
|
const scopes = this.defaultOptions.scopes.join(","); |
52
|
|
|
return this.facebookAuth.api( |
53
|
|
|
`/${facebookUserId}/?fields=${scopes}`, |
54
|
|
|
this.defaultOptions.permissions, |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected getBrowserSignInProvider() { |
59
|
|
|
return new auth.FacebookAuthProvider(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|